package cn.kgc.thread.demo2;

import java.util.concurrent.*;

/**
 * <p></p>
 *
 * @author 李孟鸠
 * @Date: 2022年12月14日8:35
 */
public class ThreadPoolDemo {
    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService =
                Executors.newScheduledThreadPool(10);
        scheduledExecutorService.schedule(()-> {
            System.out.println("线程池中执行的任务");
        },5,TimeUnit.SECONDS);

        scheduledExecutorService.shutdown();
    }

    private static void method4() {
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(()->{
            System.out.println("线程要执行的任务");
        });
    }

    private static void mehtod3() {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        executorService.execute(()->{
            System.out.println("线程要执行的任务");
        });
    }

    private static void method2() {
        //创建一个单实例的线程
//        new ThreadPoolExecutor(1, 1,
//                0L, TimeUnit.MILLISECONDS,
//                new LinkedBlockingQueue<Runnable>()))
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(()->{
            System.out.println("线程要执行的任务");
        });
    }

    private static void method1() {
        //corePoolSize:核心线程数
        //maximumPoolSize:最大线程数
        //keepAliveTime:空闲线程存活时间（超过核心线程数的线程,空闲时间）
        //线程的队列：直接交接 有界队列 无界队列
        ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(5,
                10,5,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        threadPoolExecutor.execute(()->{
            System.out.println("线程池中执行的任务:"+Thread.currentThread().getName());
        });

        //关闭线程池:shutdown关闭线程，线程池中的任务继续执行。但是此时就不能向线程池中添加任务。
        threadPoolExecutor.shutdown();
    }
}
